package com.baizhi.controller;

import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Admin;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.vo.CommonQueryPageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    UserService userService;

        /*@RequestMapping("queryAllPage/{page}/{pageSize}")
    public HashMap<String,Object> queryAllPage(@PathVariable("page") Integer pages, @PathVariable("pageSize") Integer pageSize){

        log.info("当前页：{}",pages);
        log.info("每页展示条数：{}",pageSize);

        HashMap<String, Object> map = userService.queryAllPage(pages, pageSize);

        return map;
    }*/

 /*   @RequestMapping("update")
    public HashMap<String,Object> update(@RequestBody User user){
        log.info("修改用户数据：{}",user);
        return userService.update(user);
    }*/

    /**
     * vo  （View Object）：视图对象
     * po  Persistent Object）：持久化对象
     * dto （Data Transfer Object）：数据传输对象
     * */
    @RequestMapping("queryAllPage")
    public CommonQueryPageVO queryAllPage(@RequestBody PageDTO pageDTO) {

        log.info("当前页：{}", pageDTO.getPage());
        log.info("每页展示条数：{}", pageDTO.getPageSize());

        CommonQueryPageVO commonQueryPageVO = userService.queryAllPages(pageDTO.getPage(), pageDTO.getPageSize());

        return commonQueryPageVO;
    }

    @RequestMapping("update")
    public CommonVO update(@RequestBody User user){
        log.info("修改用户数据------：{}",user);

        try {
            userService.updates(user);
            return CommonVO.success("修改成功！！！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            System.out.println("---"+e.getMessage());
            return CommonVO.faild("修改失败！！！");
        }
    }

    @RequestMapping("add")
    public CommonVO add(@RequestBody User user){
        log.info("添加用户数据：{}",user);
        try {
            userService.add(user);
            return CommonVO.success("添加成功！！！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return CommonVO.faild("添加失败！！！");
        }
    }

    @RequestMapping("uploadHeadImg")
    public HashMap<String, String> uploadHeadImg(MultipartFile headImg){

        log.info("文件名：{}",headImg.getOriginalFilename());
        log.info("文件大小：{}",headImg.getSize());
        log.info("文件类型：{}",headImg.getContentType());

        String msg = userService.uploadHeadImgAliyun(headImg);

        HashMap<String, String> map = new HashMap<>();

        map.put("fileName",msg);
        return map;
    }


    @RequestMapping("queryById")
    public User queryById(String id){
        log.info("查询管理员id：{}",id);
        return userService.queryById(id);
    }

    @RequestMapping("delete")
    public CommonVO delete(@RequestBody User user){
        log.info("修改用户数据------：{}",user);

        try {
            userService.delete(user);
            return CommonVO.success("删除成功！！！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            System.out.println("---"+e.getMessage());
            return CommonVO.faild("删除失败！！！");
        }
    }
}
